package com.wlyuan.open.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.wlyuan.area.api.IBasicAreaService;
import com.wlyuan.area.dto.AnalyzerAreaDTO;
import com.wlyuan.basic.api.IConsigneeService;
import com.wlyuan.basic.api.IReceivingAddressService;
import com.wlyuan.basic.dto.consignee.ReceivingAddressFilterDTO;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.AddressAssembler;
import com.wlyuan.open.basic.dto.OpenReceivingAddressCreationDTO;
import com.wlyuan.open.basic.dto.OpenReceivingAddressUpdateDTO;
import com.wlyuan.open.basic.vo.OpenReceivingAddressDetailVO;
import com.wlyuan.open.exception.ConsigneeNoFoundException;
import com.wlyuan.open.translator.AddressTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "收货地址")
@Validated
@RequestMapping("/api/receivingAddress")
@RestController
public class OpenReceivingAddressController {

    @DubboReference(check = false)
    private IReceivingAddressService receivingAddressService;
    @DubboReference(check = false)
    private IBasicAreaService basicAreaService;
    @DubboReference(check = false)
    private IConsigneeService consigneeService;


    @ApiOperation("创建收货地址")
    @PostMapping("/")
    @OpenApi
    public OpenReceivingAddressDetailVO createReceivingAddress(@RequestBody @Valid OpenReceivingAddressCreationDTO openReceivingCreation) {
        //校验省市区及得到编码
        var area = basicAreaService.matchArea(new AnalyzerAreaDTO(openReceivingCreation.getProvince(), openReceivingCreation.getCity(), openReceivingCreation.getDistrict()));
        var address = AddressAssembler.assemble(openReceivingCreation, area);
        //校验收货方是否存在。
        var consignee = consigneeService.getConsigneeById(openReceivingCreation.getConsigneeId(), openReceivingCreation.getTenantId());
        if (consignee == null) {
            throw new ConsigneeNoFoundException("收货方未找到");
        }
        address.setCustomerId(consignee.getCustomerId());
        var detail = receivingAddressService.create(address);
        return AddressTranslator.translate(detail);
    }

    @ApiOperation("修改收货地址")
    @PutMapping("/")
    @OpenApi
    public OpenReceivingAddressDetailVO updateReceivingAddress(@RequestBody @Valid OpenReceivingAddressUpdateDTO openUpdate) {
        //校验省市区及得到编码
        var area = basicAreaService.matchArea(new AnalyzerAreaDTO(openUpdate.getProvince(), openUpdate.getCity(), openUpdate.getDistrict()));
        var address = AddressAssembler.assemble(openUpdate, area);
        return AddressTranslator.translate(receivingAddressService.update(address));
    }


    //todo 有自己得实体类
    @PostMapping("/code/search")
    @ApiOperation("根据code获取最新收货地址")
    @OpenApi
    public OpenReceivingAddressDetailVO getAddress(@RequestBody OpenReceivingAddressCreationDTO code) {
        AssertUtils.assertNotEmpty(code.getAddressCode(), new ValueError("发货地址code不能为空"));
        ReceivingAddressFilterDTO filterDTO = new ReceivingAddressFilterDTO();
        filterDTO.setTenantId(code.getTenantId());
        filterDTO.setAddressCode(code.getAddressCode());
        filterDTO.setStatus(NamedStatus.ENABLED.getValue());
        filterDTO.setPage(1L);
        filterDTO.setSize(1L);
        var result = receivingAddressService.search(filterDTO);
        if (result != null && CollectionUtil.isNotEmpty(result.getRecords())) {
            return AddressTranslator.translate(result.getRecords().get(0));
        }
        return null;
    }

}
